package com.hehongdan.myapplication

import org.junit.Test

/**
 * 测试Kotlin集合。
 *
 * @author HHD
 * @data 20200617
 * @version v20200617
 */
class KotlinCollectionTest {

    /**
     * 双路关联。
     */
    @Test
    fun zip1() {
        val colors = listOf("red", "brown", "grey")
        val animals = listOf("fox", "bear", "wolf")
        val twoAnimals = listOf("fox", "bear")

        println(colors zip animals)
        println(colors.zip(twoAnimals))
    }

    /**
     * 双路关联。
     */
    @Test
    fun zip2() {
        val colors = listOf("red", "brown", "grey")
        val animals = listOf("fox", "bear", "wolf")

        animals.apply { }

        println(colors.zip(animals) { color, animal -> "${animal.capitalize()} 动画颜色是 $color" })
    }

    /**
     * 双路关联。
     */
    @Test
    fun zip3() {
        val colors = listOf("one", "two", "three", "three")
        val animals = listOf("fox", "bear", "wolf")

        println(colors.zip(animals) { color, animal -> "${animal.capitalize()} 动画颜色是 $color" })
    }


    /**
     * 关联。
     */
    @Test
    fun associate1() {
        val numbers = listOf("one", "two", "three", "four")
        println(numbers.associateWith { it.length })
    }

    /**
     * 关联。
     */
    @Test
    fun associate2() {
        val numbers = listOf("one", "two", "three", "four")
        println(numbers.associateBy { it.first().toUpperCase() })
        println(numbers.associateBy(keySelector = { it.first().toUpperCase() }, valueTransform = { it.length }))
    }

    /**
     * 关联。
     */
    @Test
    fun associate3() {
//        val names = listOf("Alice Adams", "Brian Brown", "Clara Campbell")
//        println(names.associate { name -> parseFullName(name).let { it.lastName to it.firstName } })
    }

    /**
     * 从头获取指定数量元素。
     */
    @Test
    fun take1() {
        val numbers = listOf("one","two","three","four","five","six")
        println(numbers.take(3))
        println(numbers.takeLast(3))
    }


    /**
     * 从头获取指定数量元素。
     */
    @Test
    fun take2() {
        val numbers = listOf("one","two","three","four","five","six")
        println(numbers.takeWhile { !it.startsWith('f') })
        println(numbers.takeLastWhile { it != "three" })
    }

    /**
     * 从尾获取指定数量元素。
     */
    @Test
    fun drop1() {
        val numbers = listOf("one","two","three","four","five","six")
        println(numbers.drop(1))
        println(numbers.dropLast(5))
    }

    /**
     * 从尾获取指定数量元素。
     */
    @Test
    fun drop2() {
        val numbers = listOf("one","two","three","four","five","six")
        println(numbers.dropWhile { it.length == 3 })
        println(numbers.dropLastWhile { it.contains('i') })
    }

    /**
     * 按区间检索集合。
     */
    @Test
    fun windowed1() {
        val numbers = listOf("one","two","three","four","five")
        println(numbers.windowed(3))
    }

    /**
     * 按区间检索集合。
     */
    @Test
    fun windowed2() {
        val numbers = (1..10).toList()
        println(numbers.windowed(3, step = 2, partialWindows = true))
        println(numbers.windowed(3) { it.sum() })
    }

    /**
     * 更新。
     */
    @Test
    fun set1() {
        val numbers = mutableListOf("one", "five", "three")
        numbers[1] = "two"
        println(numbers)
    }

    /**
     * 更新所有。
     */
    @Test
    fun set2() {
        val numbers = mutableListOf(1, 2, 3, 4)
        numbers.fill(3)
        println(numbers)
    }

    /**
     * 添加条目。
     */
    @Test
    fun add() {
        val numbersMap = mutableMapOf("one" to 1, "two" to 2)
        numbersMap.put("three", 3)
        println(numbersMap)
    }

    /**
     * 添加条目。
     */
    @Test
    fun addMap() {
        val numbersMap = mutableMapOf("one" to 1, "two" to 2)
        numbersMap.put("three", 3)
        println(numbersMap)
    }

    /**
     * 更新条目。
     */
    @Test
    fun update() {
        val numbersMap = mutableMapOf("one" to 1, "two" to 2)
        val previousValue = numbersMap.put("one", 11)
        println("键为“one”，之前的值= $previousValue，之后的值=${numbersMap["one"]}")
        println(numbersMap)

        numbersMap -= "two"
        println(numbersMap)
    }



}